package com.qianbao.finance.sync.common.support.orm.db;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.google.common.collect.Lists;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.*;

/**
 * 配置多数据源
 *
 * @author ZhouChenglin
 * @email yczclcn@163.com
 * @url www.chenlintech.com
 * @date 2017年9月3日 下午8:03:40
 */
@Configuration
@MapperScan(basePackages = {"com.qianbao.finance.sync.modules.base.dao",
        "com.qianbao.finance.sync.modules.generator.dao",
        "com.qianbao.finance.sync.modules.quartz.dao",
        "com.qianbao.finance.sync.modules.sys.dao"}, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DynamicDataSourceConfig  {

    static final String[] PACKAGE = {"com.qianbao.finance.sync.modules.base.dao",
            "com.qianbao.finance.sync.modules.generator.dao",
            "com.qianbao.finance.sync.modules.quartz.dao",
            "com.qianbao.finance.sync.modules.sys.dao"};
    static final String[] MAPPER_LOCATION =  {"classpath:mapper/base/*.xml",
            "classpath:mapper/quartz/*.xml",
            "classpath:mapper/sys/*.xml"};
    static final String MYBATIS = "classpath:config/mybatis.xml";

    @Bean(name = "masterDataSource")
    @ConfigurationProperties("spring.datasource.druid.masterDataSource")
    @Primary
    public DataSource masterDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        List<Resource> resourceList = Lists.newArrayList();
        for(String mapper:MAPPER_LOCATION){
            resourceList.addAll(Lists.newArrayList(new PathMatchingResourcePatternResolver().getResources(mapper)));
        }
        sessionFactory.setMapperLocations(resourceList.toArray(new Resource[resourceList.size()]));
        sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(MYBATIS));
        return sessionFactory.getObject();
    }

    @Bean
    public DynamicDataSource dataSource(DataSource defaultDataSource) {
        Map<String, DataSource> targetDataSources = new HashMap<>(2);
        targetDataSources.put(DataSourceEnum.MASTER.getName(), defaultDataSource);
        return new DynamicDataSource(defaultDataSource, targetDataSources);
    }
}
